import { defineStore } from 'pinia'
import { useStorage } from '@vueuse/core'

export const useUserStore = defineStore('user', {
  state: () => ({
    isAuthenticated: false,
    role: null
  }),
  actions: {
    init() {
      // 初始化逻辑
      console.log('User store initialized')
    },
    async login(credentials) {
      // 模拟登录请求
      if (credentials.username === 'admin' && credentials.password === '123456') {
        this.isAuthenticated = true
        this.role = 'admin'
      } else {
        throw new Error('Invalid credentials')
      }
    }
  }
})

export const login = (data) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      if (data.username === 'admin' && data.password === '123456') {
        resolve({
          code: 200,
          data: {
            token: 'admin-token',
            roles: ['admin']
          }
        })
      } else {
        reject(new Error('验证失败'))
      }
    }, 500)
  })
}

export const logout = () => {
  return Promise.resolve({ code: 200 })
}